﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Web.UI.WebControls;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace KoPages.Layouts.KoPages.Report
{
    public partial class GoodsReturn : LayoutsPageBase
    {
        #region Members
        #region SQL
        private string sqlMainStr = "";
        private string sqlMainStrTemplate =
@"
SELECT 
_Number
, DT
, Sklad
, Kontragent
, Sum 
FROM dbo.vw_1C_Get_Returns 
{0} 
ORDER BY DT DESC, Kontragent, Sklad
";
        #endregion SQL
        #endregion Members

        #region Page Events
        #region Page Load
        protected void Page_PreInit(object sender, EventArgs e)
        {
            //Page.MasterPageFile = "~/MyMasterPage.master";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string dtFromStr = "", dtToStr = "";
            if (IsPostBack)
            {
                #region DateTime
                DateTime dt1 = dtFrom.SelectedDate;
                if (dt1 != null)
                {
                    dtFromStr = dt1.ToString("yyyy-MM-dd");
                    hfDateFrom.Value = dtFromStr;
                }
                DateTime dt2 = dtTo.SelectedDate;
                if (dt2 != null)
                {
                    dtToStr = dt2.ToString("yyyy-MM-dd");
                    hfDateTo.Value = dtToStr;
                }
                #endregion DateTime
            }
            else
            {
                #region DateTime
                DateTime dtNow = DateTime.Now;
                DateTime dtStart = dtNow.AddMonths(-1);
                dtStart = new DateTime(dtStart.Year, dtStart.Month, 1);
                (dtFrom as DateTimeControl).SelectedDate = dtStart;
                (dtTo as DateTimeControl).SelectedDate = dtNow;
                dtFromStr = dtStart.ToString("yyyy-MM-dd");
                hfDateFrom.Value = dtFromStr;
                dtToStr = dtNow.ToString("yyyy-MM-dd");
                hfDateTo.Value = dtToStr;
                #endregion DateTime
            }
            FilterChanged();
        }
        #endregion Page Load
        #region Pre Render
        protected void Company_PreRender(object sender, EventArgs e)
        {
            if (ViewState["IsMyInit1"] == null || (bool)ViewState["IsMyInit1"] == false)
            {
                Company.Items.Insert(0, "_Все");
                ViewState["IsMyInit1"] = true;
            }
        }
        protected void Sklad_PreRender(object sender, EventArgs e)
        {
            if (ViewState["IsMyInit2"] == null || (bool)ViewState["IsMyInit2"] == false)
            {
                Sklad.Items.Insert(0, "_Все");
                //Sklad.DataBind();
                ViewState["IsMyInit2"] = true;
            }
        }
        #endregion Pre Render
        #region Control Events
        protected void Company_SelectedIndexChanged(object sender, EventArgs e)
        {
            FilterChanged();
        }
        protected void Sklad_SelectedIndexChanged(object sender, EventArgs e)
        {
            FilterChanged();
        }
        protected void dtFrom_SelectionChanged(object sender, EventArgs e)
        {
            FilterChanged();
        }
        protected void dtTo_SelectionChanged(object sender, EventArgs e)
        {
            FilterChanged();
        }
        #endregion Control Events
        #endregion Page Events

        #region Data
        protected void FilterChanged()
        {
            MakeSQL();
            SqlDS.SelectCommand = sqlMainStr;
            GW.DataBind();
        }
        protected void MakeSQL()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("WHERE ");
            sb.Append(" (DT >= CAST('" + hfDateFrom.Value + "' AS Date) )");
            sb.Append("	AND (DT <= CAST('" + hfDateTo.Value + "' AS Date) )");
            if (Company.SelectedValue != "_Все" && Company.SelectedIndex > 0)
            {
                sb.Append("	AND (Kontragent = N'" + Company.SelectedValue + "') ");
            }
            if (Sklad.SelectedValue != "_Все" && Sklad.SelectedIndex > 0)
            {
                sb.Append("	AND (Sklad = N'" + Sklad.SelectedValue + "')");
            }
            sqlMainStr = string.Format(sqlMainStrTemplate, sb.ToString());
        }
        #endregion Data
    }
}
